<template>
  <div
    v-loading="loading"
    element-loading-text="加载中..."
    style="display: flex; flex-direction: column; height: 100%"
  >
    <!-- 头部 -->
    <div class="top_app">
      <div class="top_h">
        <!-- 标题、入库单号、状态 -->
        <div class="text">
          <template v-if="newOutForm.id">
            <span class="module_color">入库详情</span>
            <span class="bound_No">{{ newOutForm?.inboundNo }}</span>
            <HStatus type="warning">待审核</HStatus>
          </template>
          <span v-else class="module_color">新增入库</span>
        </div>
        <!-- 修改状态按钮组 -->
        <div class="btns" style="display: flex; gap: 5px">
          <!-- 保存 -->
          <HPlainButton @click="onSaveAudit(1)">保存</HPlainButton>

          <!-- 审核/保存并审核 -->
          <HPlainButton type="warning" @click="onclickSave">{{
            newOutForm.id ? '审核' : '保存并审核'
          }}</HPlainButton>

          <!-- 驳回 -->
          <HPlainButton type="error" v-if="newOutForm.id" @click="rejectDialog = true"
            >驳回</HPlainButton
          >

          <!-- 取消 -->
          <HButton @click="onCancelIntb">取消</HButton>
        </div>
      </div>
    </div>

    <!-- 表单、表格 -->
    <div class="app-container" style="height: 100%">
      <div class="app-container-inner" style="display: flex; flex-direction: column; flex: 1">
        <el-form
          ref="ruleFormRef"
          :model="newOutForm"
          :rules="rulesOutStore"
          label-width="110px"
          class="newOut-ruleForm"
        >
          <el-row>
            <el-col :lg="8" :sm="12">
              <!-- 单据类型 -->
              <el-form-item label="单据类型" prop="typeId" label-width="80px">
                <el-select
                  style="width: 100%"
                  :loading="typeLoading"
                  placeholder="单据类型"
                  filterable
                  clearable
                  v-model="newOutForm.typeId"
                  :disabled="disabledInStoreOrderSpareType(newOutForm.typeId)"
                  :filter-method="changeOutType"
                  remote-show-suffix
                >
                  <el-option
                    v-for="item in outStoreType"
                    :key="item.name"
                    :value="item.id"
                    :label="item.name"
                  />
                </el-select>
              </el-form-item>

              <!-- 单据日期 -->
              <el-form-item label="单据日期" label-width="80px">
                <el-date-picker
                  v-model="newOutForm.createTime"
                  style="width: 100%"
                  type="datetime"
                  value-format="YYYY-MM-DD HH:mm"
                  :default-value="new Date()"
                />
              </el-form-item>
            </el-col>
            <el-col :lg="8" :sm="12">
              <!-- 仓库类型 -->
              <el-form-item label="仓库类型" prop="storeType">
                <el-select
                  placeholder="仓库类型"
                  disabled
                  @change="storeTypeChange"
                  v-model="newOutForm.storeType"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in storeType"
                    :key="item.title"
                    :value="item.value"
                    :label="item.title"
                  />
                </el-select>
              </el-form-item>

              <!-- 预计入库时间 -->
              <el-form-item label="预计入库时间">
                <el-date-picker
                  v-model="newOutForm.estimateTime"
                  type="datetime"
                  style="width: 100%"
                  value-format="YYYY-MM-DD HH:mm"
                  :default-value="new Date()"
                />
              </el-form-item>
            </el-col>
            <el-col :lg="8" :sm="12">
              <!-- 单据来源 -->
              <el-form-item label="单据来源">
                <el-select
                  placeholder="单据来源"
                  clearable
                  v-model="newOutForm.source"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in source"
                    :key="item.title"
                    :value="item.value"
                    :label="item.title"
                  />
                </el-select>
              </el-form-item>

              <!-- 入库备注 -->
              <el-form-item label="入库备注">
                <el-input
                  v-model="newOutForm.remark"
                  clearable
                  style="width: 100%"
                  placeholder="入库备注"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="add_good_container_btn">
          <HPlainButton @click="addGoodsList"> 添加 </HPlainButton>
        </div>
        <div style="flex: 1; position: relative">
          <el-table
            :border="true"
            :data="orderGoodsList"
            style="position: absolute"
            show-summary
            :summary-method="getSummaries"
            height="100%"
            resizable
            stripe
            :header-cell-style="tableHeaderCellStyle"
          >
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
            <!-- 序号 -->
            <el-table-column label="序号" width="70" fixed>
              <template v-slot="scope">
                <span class="order">{{ scope.$index + 1 }}</span>
                <span class="add_delete">
                  <el-icon size="16" color="#007cff" class="add" @click="addGoodsList">
                    <CirclePlus />
                  </el-icon>
                  <el-icon color="#f56c6c" size="16" @click="detelePro(scope.$index)">
                    <Delete />
                  </el-icon>
                </span>
              </template>
            </el-table-column>

            <!-- 商品详情 -->
            <el-table-column show-overflow-tooltip label="商品类型" width="82">
              <template v-slot="i">{{ i.row.type == 1 ? '成品' : '配件' }}</template>
            </el-table-column>

            <!-- 名称 -->
            <el-table-column prop="goodsName" show-overflow-tooltip label="名称" min-width="100" />

            <!-- 型号 -->
            <el-table-column prop="goodsModel" show-overflow-tooltip label="型号" min-width="100" />

            <!-- 规格 -->
            <el-table-column
              prop="goodsSpecName"
              show-overflow-tooltip
              label="规格"
              min-width="100"
            />

            <!-- 品牌 -->
            <el-table-column prop="goodsBrand" show-overflow-tooltip label="品牌" min-width="100" />

            <!-- 分类 -->
            <el-table-column prop="goodsType" show-overflow-tooltip label="分类" min-width="120" />

            <!-- 单位 -->
            <el-table-column prop="unit" show-overflow-tooltip label="单位" width="60" />

            <!-- 仓库 -->
            <el-table-column label="仓库" width="150">
              <template #header>
                仓库
                <el-popover placement="top-start" ref="popover" trigger="click" :width="160">
                  <h5 style="margin-bottom: 10px">批量填充仓库</h5>
                  <el-select
                    :teleported="false"
                    placeholder="请选择"
                    filterable
                    v-model="AllstoreId"
                    style="width: 100%"
                    :filter-method="inputValueSch"
                    remote-show-suffix
                  >
                    <el-option
                      v-for="item in modules"
                      :key="item.name"
                      :value="item.id"
                      :label="item.name"
                    />
                  </el-select>
                  <div style="display: flex; justify-content: end; margin-top: 10px">
                    <el-button
                      color="#007cff"
                      style="padding: 0 4px; height: 20px"
                      @click="changeAllStore"
                      >确定</el-button
                    >
                  </div>
                  <template #reference>
                    <span class="multiple_btn">批量</span>
                  </template>
                </el-popover>
              </template>
              <template #default="scope">
                <el-select
                  placeholder="请选择"
                  filterable
                  v-model="scope.row.storeId"
                  style="width: 100%"
                  @change="changeSeleStoreId(scope.row, scope.$index)"
                  :filter-method="inputValueSch"
                  remote-show-suffix
                >
                  <el-option
                    v-for="item in modules"
                    :key="item.name"
                    :value="item.id"
                    :label="item.name"
                  />
                </el-select>
              </template>
            </el-table-column>

            <!-- 仓位 -->
            <el-table-column label="仓位" width="150">
              <template #header>
                仓位
                <el-popover placement="top-start" ref="popover2" trigger="click" :width="160">
                  <h5 style="margin-bottom: 10px">批量填充仓位</h5>
                  <el-select
                    placeholder="请选择"
                    :teleported="false"
                    filterable
                    :disabled="!isdisabled()"
                    v-model="AllstorePositionId"
                    :filter-method="
                      (val: string) => changeSelestorePositionId(val, orderGoodsList[0]?.storeId, 0)
                    "
                    remote-show-suffix
                  >
                    <el-option
                      v-for="item in orderGoodsList[0]?.storePositionList"
                      :key="item.name"
                      :value="item.id"
                      :label="item.name"
                    />
                  </el-select>
                  <div style="display: flex; justify-content: end; margin-top: 10px">
                    <el-button
                      color="#007cff"
                      style="padding: 0 4px; height: 20px"
                      @click="changeAllstorePositionId"
                      >确定</el-button
                    >
                  </div>
                  <template #reference>
                    <span class="multiple_btn">批量</span>
                  </template>
                </el-popover>
              </template>
              <template #default="scope">
                <el-select
                  placeholder="请选择"
                  v-if="!faultsStorePositionFun(scope.row.storeId)"
                  filterable
                  clearable
                  :disabled="faultsStorePositionFun(scope.row.storeId)"
                  v-model="scope.row.storePositionId"
                  @change="changeSelestorePositionIdRow(scope.row, scope.$index)"
                  :filter-method="
                    (val: string) => changeSelestorePositionId(val, scope.row.storeId, scope.$index)
                  "
                  remote-show-suffix
                >
                  <el-option
                    v-for="item in scope.row.storePositionList"
                    :key="item.name"
                    :value="item.id"
                    :label="item.name"
                  />
                </el-select>
              </template>
            </el-table-column>

            <!-- 即时库存 -->
            <el-table-column
              prop="systemStock"
              show-overflow-tooltip
              label="即时库存"
              align="right"
              min-width="88"
            />

            <!-- 数量 -->
            <el-table-column prop="qty" show-overflow-tooltip label="数量" min-width="100">
              <template #header>
                数量
                <el-popover placement="top-start" ref="popover3" trigger="click" :width="160">
                  <h5 style="margin-bottom: 10px">批量填充数量</h5>
                  <el-input
                    placeholder="请输入"
                    v-model="Allqty"
                    @input="Allqty = Allqty.replace(/[^0-9]/g, '')"
                    @keyup.enter="changeAllqty"
                  />
                  <div style="display: flex; justify-content: end; margin-top: 10px">
                    <el-button
                      color="#007cff"
                      style="padding: 0 4px; height: 20px"
                      @click="changeAllqty"
                    >
                      确定
                    </el-button>
                  </div>
                  <template #reference>
                    <span class="multiple_btn">批量</span>
                  </template>
                </el-popover>
              </template>
              <template v-slot="item">
                <el-input
                  v-model="item.row.qty"
                  @input="item.row.qty = item.row.qty.replace(/[^0-9]/g, '')"
                />
              </template>
            </el-table-column>

            <!-- 一物一码 -->
            <el-table-column
              prop="stockCodeVos"
              show-overflow-tooltip
              label="一物一码"
              min-width="140"
            >
              <template v-slot="i">
                <HLink v-if="i.row.stockCodeVos?.length > 0" @click="getCode(i.row, i.$index)">{{
                  i.row.stockCodeVos[0].code
                }}</HLink>
                <span v-else class="change_style" @click="getCode(i.row, i.$index)">一物一码</span>
              </template>
            </el-table-column>

            <!-- 金额 -->
            <el-table-column
              prop="price"
              show-overflow-tooltip
              label="金额"
              align="right"
              width="100"
            >
              <template v-slot="i">￥{{ i.row.price.toFixed(2) }}</template>
            </el-table-column>

            <!-- 备注 -->
            <el-table-column prop="remark" show-overflow-tooltip label="备注" width="120">
              <template v-slot="i">
                <el-input type="text" v-model="i.row.remark" clearable placeholder="备注" />
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>

    <!-- 商品信息 -->
    <HModal v-model="getGoodsDialog" :width="1000" title="商品信息">
      <div class="goods_select">
        <el-container>
          <el-aside width="220px">
            <div class="main_left">
              <h4 class="titile_h4">商品类型</h4>
              <el-radio-group class="goods_type" v-model="goodsType" @change="getorderGoods">
                <el-radio :value="1">成品</el-radio>
                <el-radio :value="2">配件</el-radio>
              </el-radio-group>
              <ul class="ul_pop_up" v-loading="typeloading" element-loading-text="加载中...">
                <li
                  class="li_pop_up"
                  :style="{ color: itemIndex == null ? '#007cff' : '' }"
                  @click="onClickItem(null)"
                  >全部</li
                >
                <li
                  class="li_pop_up"
                  :style="{ color: itemIndex == item.id ? '#007cff' : '' }"
                  @click="onClickItem(item.id)"
                  v-for="(item, index) in dataType"
                  :key="index"
                  >{{ item.name }}</li
                >
              </ul>
              <div class="page_pages">
                <el-pagination
                  hide-on-single-page
                  :small="true"
                  @current-change="handleCurrentChange"
                  :current-page="currentPage"
                  :page-size="pageSize"
                  :total="goodstotal"
                  layout="total, prev, pager, next"
                />
              </div>
            </div>
          </el-aside>
          <el-main>
            <div class="main_right" v-loading="loading2" element-loading-text="加载中...">
              <div class="header">
                <el-select
                  v-model="brandId"
                  placeholder="请选择品牌"
                  filterable
                  clearable
                  reserve-keyword
                  remote-show-suffix
                  :fit-input-width="true"
                  :loading="brandLoading"
                  :filter-method="searchBrandSelect"
                  :popper-append-to-body="false"
                  @change="getorderGoods"
                  :collapse-tags="true"
                  :max-collapse-tags="2"
                  :collapse-tags-tooltip="true"
                  style="width: 10vw"
                >
                  <el-option
                    v-for="item in brandOptions"
                    :label="item.name"
                    :key="item.id"
                    :value="item.id"
                  >
                    <el-tooltip
                      v-if="item.name.length > 20"
                      :content="item.name"
                      effect="dark"
                      placement="top"
                    >
                      <span>{{ item.name }}</span>
                    </el-tooltip>
                  </el-option>
                </el-select>
                <el-input
                  clearable
                  v-model="input"
                  placeholder="商品型号/商品名称/商品规格"
                  class="input-with-select_w"
                  @keyup.enter="getorderGoods"
                >
                  <template #append>
                    <el-button :icon="Search" @click="getorderGoods" />
                  </template>
                </el-input>
              </div>
              <div class="table-reg">
                <el-table
                  :border="true"
                  stripe
                  resizable
                  :data="goodstableData"
                  @select-all="selectAllgoodsinformation"
                  ref="multipleTableRef3"
                  @select="handleClickgoodsinformation"
                  height="450px"
                >
                  <template #empty>
                    <el-empty description="暂无数据" />
                  </template>
                  <!-- 选择框 -->
                  <el-table-column show-overflow-tooltip type="selection" width="40" fixed />

                  <!-- 规格 -->
                  <el-table-column show-overflow-tooltip prop="specName" label="规格" />

                  <!-- 商品编号 -->
                  <el-table-column show-overflow-tooltip prop="code" label="商品编号" />

                  <!-- 品牌 -->
                  <el-table-column show-overflow-tooltip prop="brand" label="品牌" />

                  <!-- 名称 -->
                  <el-table-column show-overflow-tooltip prop="name" label="名称" />

                  <!-- 型号 -->
                  <el-table-column show-overflow-tooltip prop="model" label="型号" />

                  <!-- 图片 -->
                  <el-table-column show-overflow-tooltip label="图片">
                    <template v-slot="item">
                      <div class="pic" v-if="item.row.image">
                        <el-image
                          style="width: 30px; height: 30px"
                          :src="item.row.image"
                          fit="contain"
                          @click="lookPic(item.row.image)"
                        >
                          <template #error>
                            <div class="image-slot">
                              <el-icon size="28" color="#999">
                                <Picture />
                              </el-icon>
                            </div>
                          </template>
                        </el-image>
                      </div>
                    </template>
                  </el-table-column>

                  <!-- 质保时间 -->
                  <el-table-column show-overflow-tooltip prop="warrantyDay" label="质保时间(天)" />
                </el-table>

                <!-- 分页 -->
                <div class="footer">
                  <Pagination
                    ref="pagination3"
                    :list_total="typeTotal3"
                    @size-change="getorderGoods"
                    @current-change="getorderGoods"
                  />
                </div>
              </div>
            </div>
          </el-main>
        </el-container>
      </div>

      <template #footerRight>
        <HButton is-modal type="primary" @click="onCommit">确定</HButton>
        <HButton is-modal @click="onCancel">取消</HButton>
      </template>
    </HModal>

    <!-- 查看图片 -->
    <el-dialog v-model="dialogVisible" draggable title="查看图片" top="8vh">
      <img :src="lookImgUrl" style="width: 100%" />
    </el-dialog>

    <!-- 一物一码详情 -->
    <HModal v-model="getCodeDialog" title="一物一码详情" :width="1000">
      <div>
        <div class="header_code">
          <HPlainButton @click="getCodeDataShow">在库选择</HPlainButton>
          <div class="code_info">
            <p
              >出库编码总数：<span>{{ codeRowObj.qty }}</span></p
            >
            <p
              >已录入总数：<span>{{
                orderGoodsList[codeRowObj.index].stockCodeVosTemp.length
              }}</span></p
            >
            <p
              >未录入总数：<span>{{
                codeRowObj.qty - orderGoodsList[codeRowObj.index].stockCodeVosTemp.length
              }}</span></p
            >
          </div>
        </div>
        <el-table
          :border="true"
          :data="orderGoodsList[codeRowObj.index].stockCodeVosTemp"
          height="50vh"
          resizable
          stripe
          style="width: 100%"
          :header-cell-style="tableHeaderCellStyle"
        >
          <template #empty>
            <el-empty description="暂无数据" />
          </template>
          <!-- 序号 -->
          <el-table-column label="序号" width="70" fixed>
            <template v-slot="scope">
              <span class="order"> {{ scope.$index + 1 }} </span>
              <span class="add_delete">
                <el-icon size="16" color="#007cff" class="add" @click="getCodeDataShow"
                  ><CirclePlus
                /></el-icon>
                <el-icon color="#f56c6c" size="16" @click="deteleCode(scope.$index)"
                  ><Delete
                /></el-icon>
              </span>
            </template>
          </el-table-column>

          <!-- 一物一码 -->
          <el-table-column label="一物一码" prop="code" show-overflow-tooltip />

          <!-- 品牌 -->
          <el-table-column label="品牌" prop="goodsBrand" show-overflow-tooltip />

          <!-- 名称 -->
          <el-table-column label="名称" prop="goodsName" show-overflow-tooltip />

          <!-- 型号 -->
          <el-table-column label="型号" prop="goodsModel" show-overflow-tooltip />

          <!-- 规格 -->
          <el-table-column label="规格" prop="goodsSpecName" show-overflow-tooltip />

          <!-- 所属仓库 -->
          <el-table-column label="所属仓库" prop="store" show-overflow-tooltip />

          <!-- 仓位 -->
          <el-table-column label="仓位" prop="storePosition" show-overflow-tooltip />
        </el-table>
      </div>
      <template #footerRight>
        <HButton type="primary" is-modal @click="confirmCode">确定</HButton>
        <HButton is-modal @click="getCodeDialog = false">取消</HButton>
      </template>
    </HModal>

    <!-- 在库选择弹窗 -->
    <HModal v-model="SelectCodeDialog" title="在库选择" :width="1000">
      <div v-loading="getStoreCodeListLoading">
        <div class="header_code">
          <div class="left_text">
            <p
              >商品名称：<span>{{ codeRowObj.goodsName }}</span></p
            >
            <p
              >商品分类：<span>{{ codeRowObj.goodsType }}</span></p
            >
            <p
              >商品规格：<span>{{ codeRowObj.goodsSpecName }}</span></p
            >
            <p
              >商品编号：<span>{{ codeRowObj.code }}</span></p
            >
          </div>
          <div class="btn_header_code">
            <HPlainButton @click="onCreateCode">创建编码</HPlainButton>
            <el-input
              clearable
              v-model="keywordCode"
              placeholder="一物一码"
              @keyup.enter="getInStoreCodeListData"
            >
              <template #append>
                <el-button class="btn_search" :icon="Search" @click="getInStoreCodeListData" />
              </template>
            </el-input>
          </div>
        </div>
        <el-table
          :border="true"
          :data="getListCode"
          height="50vh"
          @select-all="selectAllCodeinformation"
          ref="selectCodeRef"
          @select="handleClickCodeinformation"
          resizable
          stripe
          style="width: 100%"
          :header-cell-style="tableHeaderCellStyle"
        >
          <template #empty>
            <el-empty description="暂无数据" />
          </template>
          <!-- 选择框 -->
          <el-table-column show-overflow-tooltip type="selection" width="40" fixed />

          <!-- 序号 -->
          <el-table-column label="序号" width="60" type="index" fixed></el-table-column>

          <!-- 一物一码 -->
          <el-table-column label="一物一码" prop="code" show-overflow-tooltip></el-table-column>

          <!-- 品牌 -->
          <el-table-column label="品牌" prop="goodsBrand" show-overflow-tooltip></el-table-column>

          <!-- 名称 -->
          <el-table-column label="名称" prop="goodsName" show-overflow-tooltip></el-table-column>

          <!-- 型号 -->
          <el-table-column label="型号" prop="goodsModel" show-overflow-tooltip></el-table-column>

          <!-- 规格 -->
          <el-table-column
            label="规格"
            prop="goodsSpecName"
            show-overflow-tooltip
          ></el-table-column>
          <!-- <el-table-column label="所属仓库" prop="store" show-overflow-tooltip></el-table-column>
          <el-table-column label="仓位" prop="storePosition" show-overflow-tooltip></el-table-column> -->
        </el-table>

        <!-- 分页 -->
        <div class="footer_code">
          <Pagination
            ref="paginationRef"
            :list_total="totalCode"
            @size-change="getInStoreCodeListData"
            @current-change="getInStoreCodeListData"
          />
        </div>
      </div>
      <template #footerRight>
        <HButton type="primary" is-modal @click="confirmSelectCode">确定</HButton>
        <HButton is-modal @click="SelectCodeDialog = false">取消</HButton>
      </template>
    </HModal>

    <!-- 保存并审核弹窗 -->
    <HModal v-model="checkDialog" width="auto">
      <template #header>
        <div style="color: #e69103; display: flex; align-items: center; gap: 5px">
          <el-icon size="20"> <WarningFilled /> </el-icon>审核确认
        </div>
      </template>
      <el-form>
        <el-form-item label="审核确认通过吗?">
          <el-input
            v-model="newOutForm.reviewRemark"
            maxlength="400"
            placeholder="可填写审核备注"
            type="textarea"
            class="reject_form_input"
            resize="both"
          />
        </el-form-item>
      </el-form>
      <template #footerRight>
        <HButton is-modal type="primary" @click="onSaveAudit(2)">通过</HButton>
      </template>
    </HModal>

    <!-- 驳回弹窗 -->
    <HModal v-model="rejectDialog" width="auto">
      <template #header>
        <div style="color: #f56c6c; display: flex; align-items: center; gap: 5px">
          <el-icon size="20">
            <WarningFilled />
          </el-icon>
          驳回
        </div>
      </template>

      <el-form :model="newOutForm">
        <el-form-item label="确认驳回吗?">
          <el-input
            v-model="newOutForm.reviewRemark"
            maxlength="400"
            placeholder="驳回备注"
            type="textarea"
            resize="both"
            show-word-limit
            class="reject_form_input"
          />
        </el-form-item>
      </el-form>
      <template #footerRight>
        <HPlainBgButton is-modal type="error" @click="onrejectOutbound">驳回</HPlainBgButton>
      </template>
    </HModal>

    <!-- 创建编码弹窗 -->
    <HModal v-model="CreateCodeDialog" title="创建一物一码">
      <el-form
        ref="createCodeRef"
        :model="createCode"
        :rules="CreateCoderules"
        label-width="90px"
        class="newOut-ruleForm"
      >
        <!-- 批次名称 -->
        <el-form-item label="批次名称" prop="name">
          <el-input v-model="createCode.name" placeholder="请输入批次名称" />
        </el-form-item>

        <!-- 编码类型 -->
        <el-form-item label="编码类型">
          <el-radio-group v-model="createCode.type" disabled class="ml-4">
            <el-radio :value="1">成品</el-radio>
            <el-radio :value="2">配件</el-radio>
          </el-radio-group>
        </el-form-item>

        <!-- 商品型号 -->
        <el-form-item label="商品型号">
          <el-input disabled v-model="codeRowObj.goodsModel" placeholder="商品型号" />
        </el-form-item>

        <!-- 商品规格 -->
        <el-form-item label="商品规格">
          <el-input disabled v-model="codeRowObj.goodsSpecName" placeholder="商品规格" />
        </el-form-item>

        <!-- 编码数量 -->
        <el-form-item label="编码数量">
          <el-input
            v-model="createCode.codeVos[0].qty"
            @input="createCode.codeVos[0].qty = createCode.codeVos[0].qty.replace(/[^0-9]/g, '')"
            placeholder="编码数量"
          />
        </el-form-item>

        <!-- 质保周期 -->
        <el-form-item label="质保周期">
          <el-input
            v-model="createCode.codeVos[0].warrantyDay"
            @input="
              createCode.codeVos[0].warrantyDay = createCode.codeVos[0].warrantyDay.replace(
                /[^0-9]/g,
                '',
              )
            "
            placeholder="质保周期"
          />
        </el-form-item>
      </el-form>
      <template #footerRight>
        <HButton is-modal type="primary" @click="onaddSave">确认</HButton>
        <HButton is-modal @click="CreateCodeDialog = false">取消</HButton>
      </template>
    </HModal>
  </div>
</template>

<script setup lang="ts">
  import { ref, onMounted } from 'vue'
  import { CirclePlus, Delete, WarningFilled, Picture } from '@element-plus/icons-vue'
  import useNewInt from './useNewInt'
  import Pagination from '../../../../components/Pagination/index.vue'
  import { tableHeaderCellStyle } from '@/utils/tools'
  import { addSave } from '@/api/code'
  import { ElMessage } from 'element-plus'
  import * as _ from 'lodash'
  import HStatus from '@/components/common/status/HStatus.vue'
  import HButton from '@/components/common/button/HButton.vue'
  import HPlainButton from '@/components/common/button/HPlainButton.vue'
  import HLink from '@/components/common/link/HLink.vue'
  import { HModal, HPlainBgButton } from '@/components/common'
  import pageComponentName from '@/config/page-component-name'

  defineOptions({
    name: pageComponentName.storage.inStorageCreate,
  })

  let {
    loading,
    newOutForm,
    rulesOutStore,
    onCancelIntb,
    faultsStorePositionFun,
    changeSelestorePositionId,
    source,
    detelePro,
    getStoreCodeListLoading,
    checkDialog,
    onclickSave,
    onrejectOutbound,
    storeTypeChange,
    changeOutType,
    outStoreType,
    typeLoading,
    getoutStoreTypePageSelect,
    typeloading,
    orderGoodsList,
    multipleTableRef3,
    getstorePageSelect,
    storeType,
    inputValueSch,
    modules,
    addGoodsList,
    onCommit,
    onCancel,
    itemIndex,
    getGoodsDialog,
    dialogVisible,
    lookImgUrl,
    dataType,
    onClickItem,
    handleCurrentChange,
    currentPage,
    pageSize,
    goodstotal,
    loading2,
    brandLoading,
    lookPic,
    brandId,
    searchBrandSelect,
    getorderGoods,
    pagination3,
    brandOptions,
    input,
    Search,
    goodstableData,
    selectAllgoodsinformation,
    typeTotal3,
    handleClickgoodsinformation,
    changeSeleStoreId,
    changeSelestorePositionIdRow,
    getCodeDialog,
    getCode,
    codeRowObj,
    SelectCodeDialog,
    getCodeDataShow,
    keywordCode,
    ruleFormRef,
    paginationRef,
    totalCode,
    getInStoreCodeListData,
    goodsType,
    getListCode,
    selectAllCodeinformation,
    handleClickCodeinformation,
    selectCodeRef,
    rejectDialog,
    confirmSelectCode,
    deteleCode,
    confirmCode,
    onSaveAudit,
  } = useNewInt()

  onMounted(async () => {
    new Promise(() => {
      getstorePageSelect('')
      getoutStoreTypePageSelect('')
    })
  })
  // 合计
  const getSummaries = (param) => {
    const { columns, data } = param
    const sums: string[] = []
    columns.forEach((column, index) => {
      if (index === 0) {
        sums[index] = '合计'
        return
      }
      const values = data.map((item) => Number(item[column.property]))
      if (!values.every((value) => Number.isNaN(value))) {
        if (index == 13) {
          sums[index] = `￥${values
            .reduce((prev, curr) => {
              const value = Number(curr)
              if (!Number.isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            .toFixed(2)}`
        } else if (index == 10 || index == 11) {
          sums[index] = `${values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!Number.isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)}`
        }
      } else {
        sums[index] = ''
      }
    })

    return sums
  }
  // 批量修改列表仓库
  const popover = ref() //弹窗
  const AllstoreId = ref(null)
  const changeAllStore = () => {
    popover.value.hide()
    orderGoodsList.value = orderGoodsList.value.map((o, index) => {
      changeSeleStoreId(o, index)
      return { ...o, storeId: AllstoreId.value }
    })
    orderGoodsList.value.map((o, index) => {
      changeSeleStoreId(o, index)
    })
  }
  // 批量修改列表仓位
  const popover2 = ref() //弹窗
  const AllstorePositionId = ref(null)
  const changeAllstorePositionId = () => {
    popover2.value.hide()
    //判断数组中的每一项是否都相等
    var everyResult = orderGoodsList.value.every(
      (item) => item.storeId === orderGoodsList.value[0]?.storeId,
    )
    if (everyResult) {
      orderGoodsList.value = orderGoodsList.value.map((o, index) => {
        changeSelestorePositionIdRow(o, index)
        return {
          ...o,
          storePositionList: orderGoodsList.value[0].storePositionList,
          storePositionId: AllstorePositionId.value,
        }
      })
      orderGoodsList.value.map((o, index) => {
        changeSelestorePositionIdRow(o, index)
      })
    }
  }
  // 批量修改列表数量
  const popover3 = ref() //弹窗
  const Allqty = ref(null)
  const changeAllqty = () => {
    popover3.value.hide()
    orderGoodsList.value = orderGoodsList.value.map((o) => {
      return { ...o, qty: Allqty.value }
    })
  }
  const isdisabled = () => {
    //判断数组中的每一项是否都相等
    var everyResult = orderGoodsList.value.every(
      (item) => item.storeId === orderGoodsList.value[0]?.storeId,
    )
    //  判断是否开启仓位
    var everyResult2 = modules.value.some((o) => {
      if (o.id == orderGoodsList.value[0]?.storeId) {
        return o.enableStorePosition
      }
    })
    return everyResult && everyResult2
  }

  // 创建编码
  const CreateCodeDialog = ref(false)
  const createCodeRef = ref()
  const createCode = ref({
    codeVos: [
      {
        goodsId: null, //商品ID
        goodsSpecId: null, //商品规格ID
        qty: '1', //生成编码数量
        warrantyDay: '30', //质保周期/天数
      },
    ],
    name: '', //批次名称
    qty: null, //编码数量
    type: 1, //编码类型：1-成品、2-配件
  })
  const CreateCoderules = ref({
    name: [{ required: true, message: '批次名称', trigger: ['blur', 'change'] }],
  })
  // 创建编码弹窗按钮
  const onCreateCode = () => {
    CreateCodeDialog.value = true
    createCode.value = {
      codeVos: [
        {
          goodsId: codeRowObj.value.goodsId, //商品ID
          goodsSpecId: codeRowObj.value.goodsSpecId, //商品规格ID
          qty: codeRowObj.value.qty, //生成编码数量
          warrantyDay: '30', //质保周期/天数
        },
      ],
      name: '', //批次名称
      qty: null, //编码数量
      type: codeRowObj.value.type, //编码类型：1-成品、2-配件
    }
  }
  // 保存
  const onaddSave = async () => {
    if (!createCodeRef.value) return
    await createCodeRef.value.validate((valid) => {
      if (valid) {
        createCode.value.qty = createCode.value.codeVos[0].qty
        let data = _.cloneDeep(createCode.value)

        addSave(data).then((res) => {
          if (res) {
            if (res.data.isError)
              ElMessage({
                showClose: true,
                message: res.data.message,
                duration: 3000,
                offset: 200,
                type: res.data.success ? 'success' : 'error',
              })
            if (res.data.success) {
              CreateCodeDialog.value = false
              // nextTick(() => {
              getInStoreCodeListData()
              // })
            }
          }
        })
      }
    })
  }
  // 判断入库类型是否禁用
  const disabledInStoreOrderSpareType = (id) => {
    return outStoreType.value.some((o) => {
      if (o.id == id) {
        return o.inStoreOrderSpareType
      }
    })
  }
</script>
<style scoped lang="scss">
  @import './index';
</style>
